import wilddog = require('wilddog')
import {
  Query,
  QueryOptions,
  WdObject,
  ObjectOptions,
  User
} from './index'
import { makePath } from './libs/util'
import _ = require('lodash')

export interface ConfigOptions {
  syncURL: string,
  authDomain: string
}

export class WilddogApi {

  /**
   * 
   * 
   * @type {wilddog.app.App}
   * @memberOf WilddogApi
   */
  app: wilddog.app.App
  sync: wilddog.sync.sync
  auth: wilddog.auth.Auth
  /**
   * 
   * 
   * @param {*} config 
   * @returns {WilddogApi} 
   * 
   * @memberOf WilddogApi
   */
  public init (config: ConfigOptions): WilddogApi {
    this.app = wilddog.initializeApp(config)
    this.sync = this.app.sync()
    this.auth = this.app.auth()
    return this
  }

  /**
   * 
   * 
   * @param {QueryOptions} queryOptions 
   * @returns {Query} 
   * 
   * @memberOf WilddogApi
   */
  public Query (queryOptions: QueryOptions | string | string[]): Query {
    if (!this.checkIfInited()) return
    if (_.isString(queryOptions) || _.isArray(queryOptions)) {
      return new Query({ path: queryOptions }, this)
    }
    return new Query(queryOptions, this)
  }

  /**
   * 
   * 
   * @param {(ObjectOptions | string | string[])} objOptions 
   * @returns {WdObject} 
   * 
   * @memberOf WilddogApi
   */
  public Object (objOptions: ObjectOptions | string | string[]): WdObject {
    if (!this.checkIfInited()) return
    if (_.isString(objOptions) || Array.isArray(objOptions)) {
      return new WdObject({ path: objOptions }, this)
    }
    return new WdObject(objOptions, this)
  }

  User (): User {
    if (!this.checkIfInited()) return
    return new User(this)
  }

  private checkIfInited () {
    if (!this.app) {
      throw new Error('not initialized!')
    }
    return true
  }

}
